package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.workCommand.AddWorkerCommand;
import com.woniu.command.workCommand.DeleteWorkerCommand;
import com.woniu.command.workCommand.UpdateWorkerCommand;
import com.woniu.inlet.vo.WorkerVo;
import com.woniu.outlet.po.WorkerPo;
import com.woniu.service.WorkerService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "职工管理")
@Slf4j
@RequestMapping("/worker/")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private SystemLogController systemLogController;


    @GetMapping("/workers")
    @ApiOperation("查询职工")
    public ResponseResult<List<WorkerVo>> findWorkers() throws Exception {
        List<WorkerPo> workerPos = workerService.findWorkers();
        List<WorkerVo> workerVos = new ArrayList<>();
        //对象的深拷贝
        for (WorkerPo item : workerPos) {
            WorkerVo workerVo = new WorkerVo();
            BeanUtils.copyProperties(item, workerVo);
            workerVos.add(workerVo);
        }
        systemLogController.addSystemLog("查询职工");
        return new ResponseResult<List<WorkerVo>>(workerVos, "OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workers/getById")
    @ApiOperation("根据id查询职工")
    public ResponseResult<WorkerVo> getWorkerById(Long id) throws Exception {
        WorkerPo workerPo = workerService.getWorkerById(id);

        //对象的深拷贝

        WorkerVo workerVo = new WorkerVo();
        BeanUtils.copyProperties(workerPo, workerVo);

        systemLogController.addSystemLog("根据id查询职工");
        return new ResponseResult<WorkerVo>(workerVo, "OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workers/getByCode")
    @ApiOperation("根据code查询职工id")
    public ResponseResult<Long> getWorkerByCode(@RequestParam("code") String code){
      Long id =workerService.getWorkerByCode(code);
        systemLogController.addSystemLog("根据code查询职工id");
        return new ResponseResult<Long>(id,"OK",ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workers/getByAccount")
//    @ApiOperation("根据account查询职工密码")
    public ResponseResult<String> getWorkerByAccount(String account){
       String password =workerService.getWorkerByAccount(account);
        systemLogController.addSystemLog("根据account查询职工密码");
        return new ResponseResult<String>(password,"OK",ResultCode.SUCCESS.getCode());
    }




    @PostMapping("/add")
    @ApiOperation("添加职工")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult<String> addWorker(@Valid WorkerPo workerPo, BindingResult result) {
        if(result.hasErrors()) {
            return new ResponseResult<String>(result.toString(),"参数有误",ResultCode.PARAM_TYPE_ERROR.getCode());
        }

        Command command = new AddWorkerCommand();
        boolean flag  = (boolean) command.execute(workerPo);
        if(flag){
            systemLogController.addSystemLog("添加职工");

            return new ResponseResult<String>(ResultCode.SUCCESS.getMessage(),"注册成功",ResultCode.SUCCESS.getCode());
        }


        return new ResponseResult<String>(ResultCode.COMMON_FAIL.getMessage(),"注册失败",ResultCode.COMMON_FAIL.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改职工")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateWorker(@RequestBody WorkerPo workerPo) {
        Command command = new UpdateWorkerCommand();
        command.execute(workerPo);
        systemLogController.addSystemLog("修改职工");
        return new ResponseResult<String>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除职工")
    public ResponseResult<String> deleteWorker(Long id) {
        Command command = new DeleteWorkerCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除职工");
        return new ResponseResult<String>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
